#include "SplashManager.h"
#include "ui/CocosGUI.h"
#include <cocostudio/CocoStudio.h>

#include "../../Support/GameSupport.h"
#include "../../Framework/SceneManager.h"
#include "../Menu/MenuManager.h"

using namespace cocostudio;

Scene* SplashManager::createScene()
{
	auto scene = Scene::create();
	auto layer = SplashManager::create();
	scene->addChild(layer);
	return scene;
}

bool SplashManager::init()
{
	if (!Layer::init())
	{
		return false;
	}

	setKeypadEnabled(true);

	m_screenSize = Director::getInstance()->getVisibleSize();
	initalize();

	return true;
}

void SplashManager::initalize()
{
	initBackground();
	initLayer();
	initComponent();

	this->scheduleUpdate();
}

void SplashManager::initBackground()
{
	Sprite* background = Sprite::create("bg_intro.png");
	background->setPosition(m_screenSize.width / 2, m_screenSize.height / 2);
	float scaleX = m_screenSize.width / background->getContentSize().width;
	float scaleY = m_screenSize.height / background->getContentSize().height;
	background->setScale(scaleX, scaleY);
	this->addChild(background);
}

void SplashManager::initLayer()
{
	auto node = CSLoader::getInstance()->createNode("SplashScene.csb");

	float scale = m_screenSize.height / node->getContentSize().height;
	node->setScale(scale);
	node->setAnchorPoint(Vec2(0.5f, 0.5f));
	node->setPosition(Vec2(m_screenSize.width / 2, m_screenSize.height / 2));
	node->setTag(ESplashTag::kSplashTag_SceneNode);

	Vector<Node*> list = node->getChildren();
	layerCaution = list.at(0)->getChildByName(GameSupport::getTextSplashLayer(ESplashLayer::kSplashLayer_Caution));
	layerLoading = list.at(0)->getChildByName(GameSupport::getTextSplashLayer(ESplashLayer::kSplashLayer_Loading));
	layerDesigner = list.at(0)->getChildByName(GameSupport::getTextSplashLayer(ESplashLayer::kSplashLayer_Designer));

	this->addChild(node);
}

void SplashManager::initComponent()
{
	auto node = this->getChildByTag(ESplashTag::kSplashTag_SceneNode);

	//Caution - button OK
	ui::Button* btnOK = static_cast<ui::Button*>(layerCaution
		->getChildByTag(ESplashCocoTag::kSplashCoco_Board)
		->getChildByTag(ESplashCocoTag::kSplashCoco_OK));
	btnOK->addClickEventListener(CC_CALLBACK_1(SplashManager::tapOK, this));

	//Loading - loading bar
	loadingBar = layerLoading->getChildByTag(ESplashCocoTag::kSplashCoco_ProcessbarBG)->getChildByTag(ESplashCocoTag::kSplashCoco_ProcessBarFG);
}

void SplashManager::update(float dt)
{
	ui::LoadingBar* bar = static_cast<ui::LoadingBar*>(loadingBar);
	if (bar->getPercent() != 100)
	{
		bar->setPercent(bar->getPercent() + 5);
	}
	else
	{
		auto gotoDesigner = CallFuncN::create(CC_CALLBACK_0(SplashManager::runLayerDesign, this));
		auto gotoCaution = CallFuncN::create(CC_CALLBACK_0(SplashManager::runLayerCaution, this));

		this->runAction(Sequence::create(gotoDesigner, DelayTime::create(1.5f), gotoCaution, NULL));
		this->unscheduleUpdate();
	}
}

void SplashManager::tapOK(Ref* pSender)
{
	SceneManager::getInstance()->changeScene(this, EScene::kScene_Menu);
}

void SplashManager::runLayerCaution()
{
	Point p = layerCaution->getPosition();
	layerCaution->setPosition(p.x, m_screenSize.height * 3 / 2);
	layerCaution->setVisible(true);
	layerCaution->runAction(MoveTo::create(0.5f, p));

	layerDesigner->runAction(MoveTo::create(1.5f, Vec2(layerDesigner->getPosition().x, -m_screenSize.height)));
}

void SplashManager::runLayerDesign()
{
	layerDesigner->setVisible(true);
	layerLoading->setVisible(false);
}